import Data_Save
import Descriptive_Statistics
import pylab as plt
import BaseFunction
import FindException
import numpy as np
from sklearn.decomposition import PCA
import pandas as pd  # 导入数据分析库Pandas
from scipy.interpolate import lagrange  # 导入拉格朗日插值函数
import PCA_SUPER
import seaborn as sns
import DataFrame_Operate
import Draw
from tqdm import tqdm
from sklearn.linear_model import LogisticRegression
import sklearn.metrics as metrics
from sklearn.naive_bayes import CategoricalNB
# 引入数据集，sklearn包含众多数据集
from sklearn import datasets
# 将数据分为测试集和训练集
from sklearn.model_selection import train_test_split
# 利用邻近点方式训练数据
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import Machine_Learning
from sklearn import ensemble
from sklearn.tree import DecisionTreeClassifier

BaseFunction.Correct_Show()
# 对数据进行分类
# 读取数据
# data = Data_Save.Load('data.pkl', 'card_transdata.xlsx')
data=Data_Save.Load_Save_Data('fiexd_data.pkl')
X = data.iloc[:, :-1]
Y = data.iloc[:, -1]


# # LDA
# print('======================   LDA  =======================')
# lda = LinearDiscriminantAnalysis()
# lda.fit(X, Y)
# y_pred = lda.predict(X)
# print("样本总数：\n", len(X))
# failed = np.sum(y_pred != Y)
# print("错误总数：\n", failed)
# print('正确率:\n', (len(X) - failed) / len(X))
# result_summary=metrics.classification_report(y_pred, Y)
# print(result_summary)
# accuracy1=(len(X) - failed) / len(X)
# # Data_Save.SavetoExcel([accuracy1],['LDA'],'LDA.xlsx')
# # KNN
# print('======================   KNN  =======================')
# X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3)
# knn = KNeighborsClassifier()
# # 进行填充测试数据进行训练
# knn.fit(X_train, y_train)
# params = knn.get_params()
# score = knn.score(X_test, y_test)
# print("预测得分为：%s" % score)
# print(params)
#
# y_pred = knn.predict(X)
# print("样本总数：\n", len(X))
# failed = np.sum(y_pred != Y)
# print("错误总数：\n", failed)
# print('正确率:\n', (len(X) - failed) / len(X))
# result_summary2=metrics.classification_report(y_pred, Y)
# print(result_summary2)
# accuracy2=(len(X) - failed) / len(X)
# # Data_Save.SavetoExcel([accuracy2],['KNN'],'KNN.xlsx')
#
# # 决策树
# print('======================   决策树  =======================')
# dtc = DTC(criterion='entropy')  # 建立决策树模型，基于信息熵
# dtc.fit(X, Y)  # 训练模型
# y_pred = dtc.predict(X)  # 预测
# print("样本总数：\n", len(X))
# failed = np.sum(y_pred != Y)
# print("错误总数：\n", failed)
# print('正确率:\n', (len(X) - failed) / len(X))
# result_summary3=metrics.classification_report(y_pred, Y)
# print(result_summary3)
# accuracy3=(len(X) - failed) / len(X)
# # Data_Save.SavetoExcel([accuracy3],['DT'],'DT.xlsx')
#
# # logistic
# print('======================   Logistic  =======================')
# logistic_model=LogisticRegression(C=1e5)
# logistic_model.fit(X, Y)
# y_pred = dtc.predict(X)  # 预测
# print("样本总数：\n", len(X))
# failed = np.sum(y_pred != Y)
# print("错误总数：\n", failed)
# print('正确率:\n', (len(X) - failed) / len(X))
# result_summary4=metrics.classification_report(y_pred, Y)
# print(result_summary4)
# accuracy4=(len(X) - failed) / len(X)
# # Data_Save.SavetoExcel([accuracy3],['Logistic'],'Logistic.xlsx')
#

# 集成模型
print('======================  集成模型   =======================')
bagging = ensemble.BaggingClassifier(DecisionTreeClassifier(),n_jobs=10)
bagging.fit(X, Y)
y_pred = bagging.predict(X)  # 预测
print("样本总数：\n", len(X))
failed = np.sum(y_pred != Y)
print("错误总数：\n", failed)
print('正确率:\n', (len(X) - failed) / len(X))
result_summary5=metrics.classification_report(y_pred, Y)
print(result_summary5)
accuracy5=(len(X) - failed) / len(X)
# Data_Save.SavetoExcel([accuracy3],['Logistic'],'Logistic.xlsx')


# plt.figure()
# plt.bar(x=['LDA','KNN','DT','logistic','RandomForest'],height=[accuracy1*100,90.86,accuracy3*100,accuracy4*100])
# plt.xlabel('分类器')
# plt.ylabel('正确率')
# plt.title('各分类器正确率')
# # Data_Save.SavetoExcel([result_summary,result_summary2,result_summary3,result_summary4],['LDA','KNN','决策树','logistic'],'分类结果.xlsx')
# # Data_Save.SavetoExcel([accuracy1,accuracy2,accuracy3,accuracy4],['LDA','KNN','决策树','logistic'],'分类结果.xlsx')
# r=pd.DataFrame([accuracy1,accuracy2,accuracy3,accuracy4],index=['LDA','KNN','决策树','logistic'])
# # 保存正确率
# Data_Save.SavetoExcel([r],['分类正确率'],'简单分类正确率.xlsx')
